package com.vf.admin.miaodong.controller;

import com.vf.admin.client.service.AppUserService;
import com.vf.admin.client.service.SystemAlertService;
import com.vf.admin.common.BaseController;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppCashWithdrawal;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.miaodong.service.AfterCashWithdrawalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 提现管理
 */
@Controller
@RequestMapping("/after/tx")
public class AfterTxController extends BaseController {

    @Autowired
    AfterCashWithdrawalService afterCashWithdrawalService;
    @Autowired
    AppUserService appUserService;
    @Autowired
    SystemAlertService systemAlertService;

    /**
     * 获取提现记录
     */
    @RequestMapping("/pendinglist")
    public String pendinglist(SplitPageBean splitPage, String phone, String nick, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("nick", nick);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("status", 0);
        Map<Integer, String> statusMap = Constants.getStatusMap();
        Map<Integer, String> modeMap = Constants.getModeMap();
        List<AppCashWithdrawal> list = afterCashWithdrawalService.findList(map, splitPage);
        request.setAttribute("list", list);
        request.setAttribute("statusMap", statusMap);
        request.setAttribute("modeMap", modeMap);
        request.setAttribute("splitPage", splitPage);
        request.setAttribute("phone", phone);
        request.setAttribute("nick", nick);
        request.setAttribute("startDate", startDate);
        request.setAttribute("endDate", endDate);
        return this.sucess("/web/after/tx/pendinglist");
    }

    @RequestMapping("/list")
    public String list(SplitPageBean splitPage, String phone, String nick, Integer status, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("nick", nick);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        map.put("statusNotIn", 0);
        map.put("status", status);
        Map<Integer, String> statusMap = Constants.getStatusMap();
        Map<Integer, String> modeMap = Constants.getModeMap();
        List<AppCashWithdrawal> list = afterCashWithdrawalService.findList(map, splitPage);
        request.setAttribute("list", list);
        request.setAttribute("statusMap", statusMap);
        request.setAttribute("modeMap", modeMap);
        request.setAttribute("splitPage", splitPage);
        request.setAttribute("phone", phone);
        request.setAttribute("nick", nick);
        request.setAttribute("startDate", startDate);
        request.setAttribute("endDate", endDate);
        return this.sucess("/web/after/tx/list");
    }

    /**
     * 通过
     */
    @RequestMapping("/adopt")
    @ResponseBody
    public Result adopt(Integer id) {
        try {
            AppCashWithdrawal withdrawal = afterCashWithdrawalService.get(id);
            withdrawal.setStatus(1);
            withdrawal.setYuanyin(null);
            withdrawal.setOvertime(new Date());
            afterCashWithdrawalService.save(withdrawal);

            AppUser appUser = appUserService.get(withdrawal.getUserid());
            systemAlertService.withdrawSuccess(appUser.getTengxuncode());
            return sucessJSON("成功");
        } catch (Exception e) {
            return errorJSON("失败");
        }
    }

    /**
     * 拒绝
     */
    @RequestMapping("/jujue")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Result jujue(Integer id, String reason) {
        try {
            AppCashWithdrawal withdrawal = afterCashWithdrawalService.get(id);
            if(withdrawal.getStatus() == 2) {
                return errorJSON("此提现已驳回，请勿重复提交");
            }
            withdrawal.setStatus(2);
            withdrawal.setYuanyin(reason);
            withdrawal.setOvertime(new Date());
            afterCashWithdrawalService.save(withdrawal);

            appUserService.addJinbi(withdrawal.getUserid(), withdrawal.getGoldCoin(), "金币提现驳回后返还");

            AppUser appUser = appUserService.get(withdrawal.getUserid());
            systemAlertService.withdrawReject(appUser.getTengxuncode(), reason);
            return sucessJSON("成功");
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return errorJSON("操作失败");
        }
    }

    @RequestMapping("/gotoReject")
    public String selectHour(Integer id) {
        try {
            request.setAttribute("id", id);
            return this.sucess("/web/after/tx/reject");
        } catch (Exception e) {
            return this.error("系统繁忙");
        }
    }

}
